<template>
  <view>
    <!-- 搜索框开始 -->
    <view class="index_search">
      <!-- 搜索框组件 -->
      <SearchInput></SearchInput>
    </view>
    <!-- 搜索框结束 -->
    <!-- 轮播图开始 -->
    <view class="index_swiper">
      <swiper :indicator-dots="true" :autoplay="true" :circular="true" :interval="3000" :duration="1000">
        <swiper-item v-for="(item, index) in swiperData" :key="item.goods_id">
          <view class="swiper-item">
            <navigator :url="'../../' + setURL(item.navigator_url)" :open-type="item.open_type"><image :src="item.image_src" mode="widthFix"></image></navigator>
          </view>
        </swiper-item>
      </swiper>
    </view>
    <!-- 轮播图结束 -->
    <!-- 导航开始 -->
    <view class="index_nav">
      <navigator v-for="(item, index) in navData" :key="item.name" :open-type="item.open_type == null ? 'navigate' : item.open_type" :url="'../../' + setURL(item.navigator_url)">
        <image :src="item.image_src" mode="widthFix"></image>
      </navigator>
    </view>
    <!-- 导航结束 -->
    <!-- 楼层开始 -->
    <view class="index_floor">
      <view class="floor_view" v-for="(floor, index) in floorData" :key="floor.floor_title.name">
        <view class="floor_title">
          <!-- 楼层标题图片 -->
          <image :src="floor.floor_title.image_src" mode="widthFix"></image>
        </view>
        <!-- 楼层内容 -->
        <view class="floor_product_list">
          <!-- 楼层内 -->
          <navigator
            class="floor_product"
            v-for="(product, index) in floor.product_list"
            :key="product.name"
            :open-type="product.open_type"
            :url="'../../' + setURL(product.navigator_url)"
          >
            <image :src="product.image_src" mode="widthFix"></image>
          </navigator>
        </view>
      </view>
    </view>
    <!-- 楼层结束 -->
  </view>
</template>

<script>
import { request } from '../../request/request.js';
import SearchInput from '../../components/SearchInput/SearchInput.vue';
export default {
  components: {
    SearchInput
  },
  onLoad() {
    this.getSwiperData();
    this.getNavData();
    this.getFloorData();
  },
  data() {
    return {
      swiperData: {},
      navData: {},
      floorData: {}
    };
  },
  methods: {
    /* 获取轮播图内容 */
    getSwiperData() {
      var _me = this;
      request({
        url: 'https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata'
      }).then(res => {
        // console.log(e);
        _me.swiperData = res.data.message;
        // console.log(_me.swiperData);
      });
    },
    /* 获取导航栏内容 */
    getNavData() {
      var _me = this;
      uni.request({
        url: 'https://api-hmugo-web.itheima.net/api/public/v1/home/catitems',
        success(e) {
          // console.log(e);
          _me.navData = e.data.message;
          // console.log(_me.navData);
        }
      });
    },
    /* 获取楼层内容 */
    getFloorData() {
      var _me = this;
      uni.request({
        url: 'https://api-hmugo-web.itheima.net/api/public/v1/home/floordata',
        success(e) {
          // console.log(e);
          _me.floorData = e.data.message;
          // console.log(_me.floorData);
        }
      });
    },
    setURL(url) {
      var str = url != undefined ? url : '';
      var str = str.replace('/main?', '?');
      // console.log('旧的URL', str);
      if (str == '') {
        // console.log('新的URL', 'pages/404/404');
        return 'pages/404/404';
      }
      // 寻找?
      var wenHaoIndex = str.indexOf('?');
      // 是否有？
      if (wenHaoIndex == -1) {
        // 没有问号
        wenHaoIndex = str.length;
        var wenHaoIndex = str.lastIndexOf('/', wenHaoIndex);
        // console.log('后斜杠的位置', wenHaoIndex);
        // 删除后面的内容
        str = str.slice(0, wenHaoIndex);
        // console.log(str);
        // 往前找斜杠
        var xieGangIndex = str.lastIndexOf('/', wenHaoIndex - 2);
        // console.log('前斜杠的位置', wenHaoIndex);
        var pageName = str.slice(xieGangIndex + 1, wenHaoIndex);
        // console.log('页面名', pageName);
      } else {
        // 有问号
        var urlData = str.slice(wenHaoIndex + 1, str.length);
        // console.log('问号的位置', wenHaoIndex);
        // console.log('参数', urlData);
        // 往前找斜杠
        var xieGangIndex = str.lastIndexOf('/', wenHaoIndex);
        // console.log('斜杠的位置', wenHaoIndex);
        var pageName = str.slice(xieGangIndex + 1, wenHaoIndex);
        // console.log('页面名', pageName);
      }

      // 开始拼接新的url
      // 是否有参数
      if (urlData == undefined) {
        // 没参数
        str = 'pages/' + pageName + '/' + pageName;
      } else {
        // 有参数
        str = 'pages/' + pageName + '/' + pageName + '?' + urlData;
      }
      // console.log('新的URL', str);
      return str;
    }
  }
};
</script>

<style lang="scss">
/* 轮播图开始 */
.index_swiper {
  swiper {
    width: 750rpx;
    height: 340rpx !important ;
    // height: calc(750rpx / 750 * 340);
    swiper-item {
      .swiper-item {
        navigator {
          image {
            width: 100%;
            height: 100%;
          }
        }
      }
    }
  }
}
/* 轮播图结束 */
/* 导航开始 */
.index_nav {
  navigator {
    display: inline-block;
    width: calc(100% / 4);
    height: calc(750rpx / 4 / 128 * 140);
    image {
      width: 100%;
      height: 100%;
    }
  }
}
/* 导航结束 */
/* 楼层开始 */
.index_floor {
  .floor_view {
    .floor_title {
      width: 750rpx;
      height: 59rpx;
      image {
        width: 100%;
        height: 100%;
      }
    }
    .floor_product_list {
      position: relative;
      height: 400rpx;
      navigator {
        float: left;
        width: 233rpx;
        height: 386rpx;
        margin: 6rpx;
        &:nth-last-child(-n + 4) {
          float: right;
          width: 232rpx;
          height: 188rpx;
        }
        image {
          width: 100%;
          height: 100% !important;
        }
      }
    }
  }
}
/* 楼层结束 */
</style>
